//
//  ShipingBFView.m
//  SelfDriving
//
//  Created by apple on 17/1/14.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ShipingBFView.h"
#import "AudioMsgPlayer.h"

@interface ShipingBFView ()<ZFPlayerDelegate>
//进度条(托控件)
//监控进度
@property (nonatomic,strong)NSTimer *avTimer;

@property (weak, nonatomic) IBOutlet ZFPlayerView *playerView;
@property (weak, nonatomic) IBOutlet UISlider *progress;
@property (weak, nonatomic) IBOutlet UIButton *bofangbtn;
@end
@implementation ShipingBFView
{
    MPMoviePlayerController *moviePlayer;
    AVPlayer *_player;
    AVPlayerItem *_playItem;
    AVPlayerLayer *_playerLayer;
    AVPlayerLayer *_fullPlayer;
    int isplay;

}
- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark -- 新版
    
    // 初始化控制层view(可自定义)
    ZFPlayerControlView *controlView = [ZFPlayerControlView new];
    
    
    // 初始化播放模型
    ZFPlayerModel *playerModel = [[ZFPlayerModel alloc] init];
    // playerView的父视图
    playerModel.fatherView = self.view;
    playerModel.videoURL = [NSURL URLWithString:_url];
    playerModel.title = @"播放视频中。。。";
    
    [self.playerView playerControlView:controlView playerModel:playerModel];
    // 设置代理
    self.playerView.delegate = self;
    // 自动播放
    [self.playerView autoPlayTheVideo];
//    [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view).offset(80);
//        make.left.right.equalTo(self.view);
//        // 这里宽高比16：9，可以自定义视频宽高比
//        make.height.equalTo(self.playerView.mas_width).multipliedBy(9.0f/16.0f);
//    }];
//    self.playerView.frame = XYWH(0,20,SCREEN_WIDTH,SCRE_HEIGHT-20);
    
    
#pragma mark -- 旧版
    /*
    self.view.backgroundColor = [UIColor blackColor];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    [btn setTitle:@"X" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    
    
    NSURL *url = [NSURL URLWithString:_url];
    _playItem = [AVPlayerItem playerItemWithURL:url];
    _player = [AVPlayer playerWithPlayerItem:_playItem];
    _playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    _playerLayer.frame = CGRectMake(0, SCRE_HEIGHT/2-SCRE_WIDTH/2, SCRE_WIDTH, SCRE_WIDTH);
    _playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;//视频填充模式
    [self.view.layer addSublayer:_playerLayer];
    [_progress setThumbImage:[UIImage imageNamed:@"图标---播放进行圆"] forState:UIControlStateNormal];

    CGRect r =  _progress.frame;
    r.origin.y = _playerLayer.frame.origin.y + _playerLayer.frame.size.height + 20;
    _progress.frame = r;
    
   CGRect f = _bofangbtn.frame;
    f.origin.y = r.origin.y+5;
    _bofangbtn.frame = f;
    //监控播放进度
    self.avTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timer)userInfo:nil repeats:YES];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished1:)name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    
    */

}


-(void)zf_playerBackAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarHidden = NO;
}
/*
//开始
- (IBAction)play:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    if (isplay==0) {
        [_player play];
        isplay = 1;
    }else{
        isplay = 0;
        [_player pause];
    }
}

//监控播放进度方法
- (void)timer
{
    self.progress.value = CMTimeGetSeconds(_player.currentItem.currentTime) / CMTimeGetSeconds(_player.currentItem.duration);
    float total = CMTimeGetSeconds([_playItem duration]);
    float current = CMTimeGetSeconds(_player.currentItem.currentTime);
}
-(void)playbackFinished1:(NSNotification *)notification
{
    AVPlayerItem * p = [notification object];
    //关键代码
    [p seekToTime:kCMTimeZero];
//    [_player play];
}
-(void)backAction:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
 - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}



@end
